//
//  ViewController.m
//  CollectionViewTest
//
//  Created by cnbizMedia on 16/4/14.
//  Copyright © 2016年 wen. All rights reserved.
//

#import "ViewController.h"
#import "MyHeadView.h"
#import "Masonry.h"
#import "RecipeCollectionHeaderView.h"
@interface ViewController ()<UICollectionViewDelegateFlowLayout>
{


    __weak IBOutlet UICollectionView *myCollectionView;
    
    float acc;
//    UICollectionViewCell *cell;
}
@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    [myCollectionView reloadData];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    myCollectionView.backgroundColor = [UIColor lightGrayColor];
    myCollectionView.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.1];
    [self->myCollectionView registerClass:[MyHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"hxwHeader"];
//    [self->myCollectionView registerClass:[MyHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"hxwHeader"];
    
    acc = (self.view.frame.size.width - 1) / 2 ;

    NSLog(@"ACC %f",acc);
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"yourCell" forIndexPath:indexPath];
    
   
//    NSLog(@"cell %@",cell);
//    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:1];
//    UILabel*label=(UILabel*)[cell viewWithTag:2];
//    NSLog(@"recipeImageView %@",recipeImageView);
//    NSLog(@"label %@",label);
    
//    recipeImageView.image = [UIImage imageNamed:@"121.jpg"];
//    label.text = @"sb";
    
    
    cell.contentView.layer.borderWidth = 0.5f;
    cell.contentView.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor blackColor]);
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:18];
    label.textColor = [UIColor blackColor];
    label.text = @"男款商务手表";
//    label.textAlignment = NSTextAlignmentCenter;
    
    UILabel *moneyLabel = [[UILabel alloc] init];
    moneyLabel.font = [UIFont systemFontOfSize:15];
    moneyLabel.textColor = [UIColor redColor];
    moneyLabel.text = @"$888";
    
    NSString *oldPrice = @"¥ 12345";
    NSUInteger length = [oldPrice length];
    
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:oldPrice];
    [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(2, length-2)];
//    [attri addAttribute:NSStrikethroughColorAttributeName value:UIColorFromRGB(0x999999, 1) range:NSMakeRange(2, length-2)];
    [attri addAttribute:NSStrikethroughColorAttributeName value:[UIColor grayColor] range:NSMakeRange(2, length-2)];
    
    
    UILabel *oldMoneyLabel = [[UILabel alloc] init];
    oldMoneyLabel.font = [UIFont systemFontOfSize:15];
    oldMoneyLabel.textColor = [UIColor grayColor];
//    oldMoneyLabel.text = @"$998";
    [oldMoneyLabel setAttributedText:attri]; //label显示的字体上是否需要加删除线
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"121.jpg"]];
    
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    [cell.contentView addSubview:label];
    [cell.contentView addSubview:imageView];
    [cell.contentView addSubview:moneyLabel];
    [cell.contentView addSubview:oldMoneyLabel];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(cell);
        make.top.equalTo(cell.contentView.mas_top).with.offset(5);
        make.size.mas_equalTo(CGSizeMake(133, 100));
    }];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(cell.contentView.mas_centerX);
//        make.bottom.equalTo(cell.contentView).with.offset(-10);
        make.top.equalTo(imageView.mas_bottom).with.offset(4);
        make.left.equalTo(imageView.mas_left);
//        make.right.equalTo(imageView.mas_right);
        make.size.mas_equalTo(CGSizeMake(133, 40));
    }];
    
    [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell.mas_bottom).with.offset(-20);
        make.left.equalTo(imageView.mas_left);
//        make.right.equalTo(imageView.mas_right);
    }];
    
    [oldMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell.mas_bottom).with.offset(-20);
//        make.left.equalTo(imageView.mas_left);
        make.right.equalTo(imageView.mas_right);
    }];
    
    if(indexPath.section==0)
    {
        cell.backgroundColor = [UIColor whiteColor];
        
        UIView* selectedBGView = [[UIView alloc] initWithFrame:cell.bounds];
        selectedBGView.backgroundColor = [UIColor whiteColor];
        cell.selectedBackgroundView = selectedBGView;
        
     
    }
    else if(indexPath.section==1)
    {
        cell.backgroundColor = [UIColor whiteColor];
        
        UIView* selectedBGView = [[UIView alloc] initWithFrame:cell.bounds];
        selectedBGView.backgroundColor = [UIColor whiteColor];
        cell.selectedBackgroundView = selectedBGView;
    }
    
    
    
    
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    CGSize size = { 400,50};
    return size;
}

//-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
//{
//    CGSize size = {240,25};
//    return size;
//}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
     MyHeadView *headView;
    
    UICollectionReusableView *reusableview = nil;
    
    if (kind == UICollectionElementKindSectionHeader){
        
        NSLog(@"HEAD indexPath  %@",indexPath);
        
        RecipeCollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"yourHead" forIndexPath:indexPath];
 
        UILabel *abc = [[UILabel alloc]init];
        [abc setFrame:CGRectMake(0, 0, 100, 30)];
        abc.text = @"东东凌东呛";
//        headerView.title.text = @"大河向东流";
        [headerView addSubview:abc];
        reusableview = headerView;
        
//        headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"hxwHeader" forIndexPath:indexPath];
//        [headView setLabelText:[NSString stringWithFormat:@"section %ld's header",(long)indexPath.section]];
        
    }
//    else if([kind isEqual:UICollectionElementKindSectionFooter])
//    {
//        headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"hxwHeader" forIndexPath:indexPath];
//        [headView setLabelText:[NSString stringWithFormat:@"section %ld's footer",(long)indexPath.section]];
//    }
    
    
    return reusableview;
}




- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    if(indexPath.section==0 && indexPath.row==1)
//    {
//        return CGSizeMake(50, 50);
//    }
//    else
//    {
//        return CGSizeMake(75, 30);
//    }

    return CGSizeMake( acc, 160);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if(section==0)
    {
//        return UIEdgeInsetsMake(35, 25, 15, 25);
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }
    else
    {
//        return UIEdgeInsetsMake(15, 15, 15, 15);
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }
}

//各部分的行距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    if(section==0)
    {
        return 1;
    }
    else
    {
        return 1;
    }
}

//各部分的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    if(section==0)
    {
        return 1;
    }
    else
    {
        return 1 ;
    }
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    [self.myArray removeObjectAtIndex:indexPath.row];
    
//    [collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
    NSLog(@"正在点击	");
}



@end
